package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ActivityAdapter extends RecyclerView.Adapter<ActivityAdapter.ViewHolder> {

    private List<ActivityInfo> mActivityList;

    // 构造方法：接收活动数据列表
    public ActivityAdapter(List<ActivityInfo> activityList) {
        mActivityList = activityList;
    }

    // 加载列表项布局
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_event, parent, false);
        return new ViewHolder(view);
    }

    // 绑定数据到布局控件
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ActivityInfo activity = mActivityList.get(position);
        holder.idText.setText("活动编号: " + activity.getId());
        holder.nameText.setText(activity.getName());
        holder.timeLocationText.setText(activity.getDate() + " | " + activity.getLocation());
        holder.participantsText.setText("参与人数: " + activity.getParticipantCount() + "人");
    }

    // 返回列表项数量
    @Override
    public int getItemCount() {
        return mActivityList.size();
    }

    // ViewHolder：持有布局中控件的引用
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView idText;
        TextView nameText;
        TextView timeLocationText;
        TextView participantsText;

        ViewHolder(View view) {
            super(view);
            idText = view.findViewById(R.id.tv_item_event_id);
            nameText = view.findViewById(R.id.tv_item_event_name);
            timeLocationText = view.findViewById(R.id.tv_item_event_time_location);
            participantsText = view.findViewById(R.id.tv_item_event_participants);
        }
    }
}